// import pinyin from 'js-pinyin'

Date.prototype.format = function (fmt) {
    let o = {
        'M+': this.getMonth() + 1,                 // 月份
        'd+': this.getDate(),                    // 日
        'h+': this.getHours(),                   // 小时
        'm+': this.getMinutes(),                 // 分
        's+': this.getSeconds(),                 // 秒
        'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
        S: this.getMilliseconds()             // 毫秒
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (let k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) {
            fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
        }
    }
    return fmt;
}

// 传入日期，获取上一个物料填报月（21号到下个月20号，算做本月）
export function getRequireDate(date) {
    let yMonth = '';
    let curMonth = date.getMonth();
    if (date.getDate() <= 20) {
        if (date.getMonth === 0) {
            yMonth = (date.getFullYear() - 1) + '-12';
        } else {
            yMonth = date.getFullYear() + '-' + (curMonth < 10 ? '0' + curMonth.toString() : curMonth.toString());
        }
    } else {
        yMonth = date.format('yyyy-MM');
    }
    return yMonth;
}

export function getDate(strDate) {
    let date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/,
        (a) => parseInt(a, 10) - 1).match(/\d+/g) + ')');
    return date;
}

export function getPatternTime(pattern) {
    const date = new Date();

    const options = {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit',
        hour12: false
    };

    // 替换pattern中的字符，生成对应的格式
    const formatPattern = pattern.replace('yyyy', date.getFullYear())
        .replace('MM', String(date.getMonth() + 1).padStart(2, '0'))
        .replace('dd', String(date.getDate()).padStart(2, '0'))
        .replace('HH', String(date.getHours()).padStart(2, '0'))
        .replace('mm', String(date.getMinutes()).padStart(2, '0'))
        .replace('ss', String(date.getSeconds()).padStart(2, '0'));

    return formatPattern;
}

export function parseTime(time) {
    const date = new Date(time);
    const weekDays = ['日', '一', '二', '三', '四', '五', '六'];
    const year = date.getFullYear();
    const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
    const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    const hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
    const minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    const second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    const dayOfWeek = weekDays[date.getDay()];
    return `${year}年${month}月${day}日 ${hour}:${minute}:${second}`;
}

export function parseTime2(time) {
    const date = new Date(time);
    const weekDays = ['日', '一', '二', '三', '四', '五', '六'];
    const year = date.getFullYear();
    const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
    const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    const hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
    const minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    const second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    const dayOfWeek = weekDays[date.getDay()];
    return `${year}年${month}月${day}日`;
}

export function getDaysDifference(time) {
    console.log(time)
    // 将传入的时间转换为 Date 对象
    const inputDate = new Date(time);
    // 获取当前时间的零点
    const currentDate = new Date();
    currentDate.setHours(0, 0, 0, 0);
    // 计算时间差（毫秒）
    const timeDiff = currentDate - inputDate;
    // 转换为天数
    return Math.floor(timeDiff / (1000 * 60 * 60 * 24));
}

export function parseTime3(time) {
    const date = new Date(time);
    const year = date.getFullYear();
    const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
    const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    return `${month}/${day}`;
}

export function parseTime4(time) {
    const date = new Date(time);
    const weekDays = ['日', '一', '二', '三', '四', '五', '六'];
    const year = date.getFullYear();
    const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
    const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    const hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
    const minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    const second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    const dayOfWeek = weekDays[date.getDay()];
    return `${year}${month}${day}${hour}${minute}${minute}${second}`;
}export function parseTime5(time) {
    const date = new Date(time);
    const weekDays = ['日', '一', '二', '三', '四', '五', '六'];
    const year = date.getFullYear();
    const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
    const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    const hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
    const minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    const second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    const dayOfWeek = weekDays[date.getDay()];
    return `${year}-${month}-${day}`;
}